package com.atguigu.day08;

import com.atguigu.bean.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.api.common.state.MapState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.util.ArrayList;
import java.util.Comparator;

public class Flink06_KeyedState_MapState {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //2.从端口读取数据
        SingleOutputStreamOperator<WaterSensor> waterSensorStream = env.socketTextStream("localhost", 9999)
                .map(new MapFunction<String, WaterSensor>() {
                    @Override
                    public WaterSensor map(String value) throws Exception {
                        String[] split = value.split(",");
                        return new WaterSensor(split[0], Long.parseLong(split[1]), Integer.parseInt(split[2]));
                    }
                });

        //3.将相同id的数据聚合到一块
        KeyedStream<WaterSensor, Tuple> keyedStream = waterSensorStream.keyBy("id");

        //4.统计每种传感器每种水位值出现的次数
        keyedStream.process(new KeyedProcessFunction<Tuple, WaterSensor, String>() {

            //TODO 1.定义状态  k:vc v:vc对应的个数
            MapState<Integer, Integer> mapState;

            @Override
            public void open(Configuration parameters) throws Exception {
                //TODO 2.初始化状态
                mapState = getRuntimeContext().getMapState(new MapStateDescriptor<Integer, Integer>("mapState", Integer.class, Integer.class));
            }

            @Override
            public void processElement(WaterSensor value, KeyedProcessFunction<Tuple, WaterSensor, String>.Context ctx, Collector<String> out) throws Exception {
                //TODO 3.使用状态
                //1.先判断当前这个水位值是第一次来还是不是第一次来
                if (mapState.contains(value.getVc())){
                    //不是第一次来  取出之前的计算结果
                    Integer lastCount = mapState.get(value.getVc());
                    //更新状态
                    mapState.put(value.getVc(),lastCount+1);
                }else {
                    //第一次来
                    mapState.put(value.getVc(),1);
                }

                //获取MapState中保存的结果并输出
                out.collect(mapState.entries().toString());

            }
        }).print();

        env.execute();
    }

}
